//
//  KTTabBarView.m
//  KTLotteryTicket
//
//  Created by CoolKi on 16/3/4.
//  Copyright © 2016年 CoolKi. All rights reserved.
//

#import "KTTabBarView.h"
#import "KTTabBarBtn.h"

@interface KTTabBarView ()
@property (nonatomic,weak)UIButton * selectedBtn;

@end

@implementation KTTabBarView



- (void)setTabBarItemArr:(NSArray *)tabBarItemArr{
    _tabBarItemArr = tabBarItemArr;
    
    for (int i = 0; i < tabBarItemArr.count; i++) {
        
        UITabBarItem * item = tabBarItemArr[i];
        
        
        KTTabBarBtn * barBtn = [[KTTabBarBtn alloc]init];
        [barBtn setImage:item.image forState:UIControlStateNormal];
        [barBtn setImage:item.selectedImage forState:UIControlStateSelected];
        barBtn.backgroundColor = [UIColor purpleColor];
        barBtn.tag = i;
        
        [barBtn addTarget:self action:@selector(whichVCWithBtn:) forControlEvents:UIControlEventTouchDown];
        if (i==0) {
            [self whichVCWithBtn:barBtn];
        }
        [self addSubview:barBtn];
        
    }
}

//点击按钮时调用
- (void)whichVCWithBtn:(UIButton *)button{
    
    self.selectedBtn.selected = NO;
    button.selected = YES;
    self.selectedBtn = button;
    
    if ([self.delegate respondsToSelector:@selector(tabBarView:Index:)]) {
        [self.delegate tabBarView:self Index:button.tag];
    }
}


-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = self.bounds.size.width/self.subviews.count;
    CGFloat height = self.bounds.size.height;
    
    for (UIButton * button in self.subviews) {
        x = button.tag * width;
        button.frame = CGRectMake(x, y, width, height);
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
